package com.lanxin.judge.controller;


import com.lanxin.hr.entity.ZzAccount;
import com.lanxin.hr.entity.ZzEmployee;
import com.lanxin.hr.service.IZzEmployeeService;
import com.lanxin.judge.Vo.ApplyVo;
import com.lanxin.judge.Vo.ApprovalVo;
import com.lanxin.judge.entity.LcApproval;
import com.lanxin.judge.entity.LcFlow;
import com.lanxin.judge.service.ILcApprovalService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author author
 * @since 2025-08-11
 */
@Controller
@RequestMapping("judge/approval")
public class LcApprovalController {

    @Autowired
    private ILcApprovalService approvalService;

    @Autowired
    private IZzEmployeeService employeeService;

    @RequestMapping("/listMyApproval")
    //获取我需要审批的单据（报销申请，资产购买申请，资产移动申请）
    public ModelAndView getMyApprovals(HttpSession session) {
        ModelAndView modelAndView = new ModelAndView();
        ZzAccount account = (ZzAccount) session.getAttribute("account");
        List<ApplyVo> myApprovals = approvalService.getMyApprovals(account);
        session.setAttribute("myApprovals", myApprovals);
        LcFlow.DocType[] docTypes = LcFlow.DocType.values();
        modelAndView.addObject("docTypes",docTypes);
        modelAndView.addObject("myApprovals", myApprovals);
        modelAndView.setViewName("judge/listMyApproval");
        return modelAndView;
    }

    @RequestMapping("/toApproval")
    //oid是审批申请的oid
    public ModelAndView toApproval(@RequestParam Integer oid, HttpSession session) {
        ModelAndView modelAndView = new ModelAndView();
        //获取我需要审批的单据
        List<ApplyVo> applyVos = (List<ApplyVo>)session.getAttribute("myApprovals");
        LcFlow.DocType[] docTypes = LcFlow.DocType.values();
        ApplyVo applyVo = new ApplyVo();
        //获取当前审批的单据
        for(ApplyVo myApproval : applyVos){
            if(myApproval.getOid().equals(oid)){
                applyVo = myApproval;
            }
        }
        //根据申请单编号获取审批记录，用于查看审批记录
        List<ApprovalVo> approvalVos = approvalService.getApprovalByDocNo(applyVo.getDocNo());
        modelAndView.addObject("approvalVos",approvalVos);
        modelAndView.addObject("oid",oid);
        modelAndView.addObject("myApproval",applyVo);
        modelAndView.addObject("docTypes",docTypes);
        modelAndView.setViewName("judge/toApproval");
        return modelAndView;
    }

    //进行审批
    @RequestMapping("/doApproval")
    //oid为审批申请的oid
    public String doApproval(HttpSession session,@RequestParam Integer oid,@RequestParam String status,@RequestParam String opinion) {
        try {
            ZzAccount account = (ZzAccount) session.getAttribute("account");
            ZzEmployee employee = employeeService.getEmpById(account.getAid());
            List<ApplyVo> myApprovals = (List<ApplyVo>)session.getAttribute("myApprovals");
            ApplyVo applyVo = new ApplyVo();//获取当前审批的申请
            for(ApplyVo myApproval : myApprovals){
                if(myApproval.getOid().equals(oid)){
                    applyVo = myApproval;
                }
            }
            approvalService.doApproval(employee,applyVo,oid,status,opinion);
        }catch (Exception e){
            e.printStackTrace();
            throw new RuntimeException("审批失败");
        }
        return "redirect:listMyApproval";
    }

    //获取我的所有申请单
    @RequestMapping("/listMyDoc")
    public ModelAndView listMyDoc(HttpSession session) {
        ModelAndView modelAndView = new ModelAndView();
        ZzAccount account = (ZzAccount) session.getAttribute("account");
        ZzEmployee employee = employeeService.getEmpById(account.getAid());
        List<ApplyVo> docs = approvalService.listMyDoc(employee);
        modelAndView.addObject("docs",docs);
        modelAndView.setViewName("judge/listMyDoc");
        return modelAndView;
    }

    //获取审批历史
    @RequestMapping("/getApprovalLog")
    public String getApprovalLog(@RequestParam String docNo,Model model){
        List<ApprovalVo> approvalVos = approvalService.getApprovalByDocNo(docNo);
        model.addAttribute("approvalVos",approvalVos);
        return "judge/approvalLog";
    }
}
